// pages/menu/add-material/location-material/location-material.js
var verify = require('../../../../utils/verify-token.js')
var app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   // console.log(options)
    let that = this;
		let deviceId = options.deviceId;
		let bunkerId = options.bunkerId;
    let organId = options.organId;
    let meterialId = options.meterialId;
    let unit = options.unit;
	//	console.log(deviceId)
   // console.log(bunkerId)
    that.setData({
      sh: app.globalData.screenHeight,
      sw: app.globalData.screenWidth,
			deviceId:deviceId,
			bunkerId:bunkerId,
      organId: organId,
      meterialId: meterialId,
      unit: unit,
			materiallist:[]
    })
		that.getMaterial();
  },
	getMaterial:function(){
		let that = this;
		let baseUrl = app.globalData.baseUrl;
		let deviceId = that.data.deviceId;
		let token = app.globalData.token;
		wx.request({
		  url:baseUrl+ 'applet/location/material/list?deviceId='+deviceId,
		  header:{
		    'AUTHORIZATION':token
		  },
		  success:function(res){
		    if (res.data.code == 60006) {
		      verify.clearAll()
		    } else if(res.data.code==200){
					that.setData({
						materiallist:res.data.data
					})
		    } else {
		      wx.showModal({
		        title: '操作失败',
		        content: res.data.message,
		      })
		    }
		  }
		})
	},

  select:function(e){
    let that = this;
		let locationMaterialId = e.currentTarget.dataset.item;
    wx.showModal({
      title: '补料',
      content: '确定要补料吗？',
      success:function(res){
        if (res.confirm){
         that.addComfirm(locationMaterialId);
        }
      }
    })
  },
  //补料
  addComfirm:function(locationMaterialId){
		let that = this;
		let deviceId = that.data.deviceId;
		let bunkerId = that.data.bunkerId;
		let baseUrl = app.globalData.baseUrl;
		let token = app.globalData.token;
		wx.request({
		  url:baseUrl+ 'applet/location/material',
		  header:{
		    'AUTHORIZATION':token,
				'content-type':'application/x-www-form-urlencoded'
		  },
			method:"post",
			data:{
				deviceId:deviceId,
				bunkerId:bunkerId,
				locationMaterialId:locationMaterialId
			},			
		  success:function(res){
		    if (res.data.code == 60006) {
		      verify.clearAll()
		    } else if(res.data.code==200){
					wx.showToast({
					  title: '补料指令下发成功',
					})
					that.getMaterial();
		    } else {
		      wx.showModal({
		        title: '操作失败',
		        content: res.data.message,
		      })
		    }
		  }
		})
    
  },
  // 清空余料
  clear: function (res) {
    let that = this;
    wx.showModal({
      title: '清空料仓',
      content: '确定要清空该料仓吗？',
      showCancel: true,
      success: function (res) {
        if (res.cancel) {
          console.log('取消清空')
        } else {
          that.clearBunker();
        }
      },
      fail: function (res) {

      }
    })
  },
  clearBunker: function () {
    let that = this;
    let deviceId = that.data.deviceId;
    let bunkerId = that.data.bunkerId;
    let token = app.globalData.token;
    let data = {
      deviceId: that.data.deviceId,
      bunkerId: that.data.bunkerId,
      organId: that.data.organId,
      meterialId: that.data.meterialId,
      unit: that.data.unit
    };
    wx.request({
      url: app.globalData.baseUrl + 'device/' + deviceId + '/' + bunkerId + '/clearBunker',
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
        'AUTHORIZATION': token
      },
      method: 'post',
      data: data,
      success: function (res) {
        if (res.data.code == 60006) {
          verify.clearAll()
        } else if (res.data.code == 200) {
          wx.showToast({
            title: '操作成功',
            icon: 'success',
            duration: 2000
          })
        } else {
          wx.showToast({
            title: '操作失败',
            icon: 'fail',
            duration: 2000
          })
        }
      },
      fail() {
        console.log("清空失败")
      }
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})